package vn.evolus.pencil.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.SaltSource;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.userdetails.UserDetailsService;

import vn.evolus.commons.data.IRepository;
import vn.evolus.pencil.initialization.InitDataService;
import vn.evolus.pencil.service.ItemCollectionService;
import vn.evolus.pencil.service.RoleService;
import vn.evolus.pencil.service.UploadDataService;
import vn.evolus.pencil.service.UserService;
import vn.evolus.pencil.service.impl.ItemCollectionServiceImpl;
import vn.evolus.pencil.service.impl.PencilUserDetailsServiceImpl;
import vn.evolus.pencil.service.impl.RoleServiceImpl;
import vn.evolus.pencil.service.impl.UploadDataServiceImpl;
import vn.evolus.pencil.service.impl.UserServiceImpl;


@Configuration
public class BeanConfig {
	
	@Value("${pencil.upload.folder}")
	private String uploadFolder;
	
	@Autowired
    private IRepository repository;
	@Autowired
	private PasswordEncoder passwordEncoder;
	@Autowired
	private SaltSource saltSource;
	
	@Bean
	public InitDataService initTestService(){
		return new InitDataService(userService(), roleService());
	}
	
	@Bean
	public ItemCollectionService itemCollectionService(){
		return new ItemCollectionServiceImpl(uploadFolder, repository);
	}
	
	@Bean
	public RoleService roleService(){
		return new RoleServiceImpl(repository);
	}

	@Bean
	public UserService userService(){
		return new UserServiceImpl(passwordEncoder, saltSource, repository, roleService());
	}
	
	@Bean
	public UploadDataService uploadDataService(){
		return new UploadDataServiceImpl(uploadFolder);
	}
	
	@Bean
	public UserDetailsService userDetailsService(){
		return new PencilUserDetailsServiceImpl(repository);
	}
}
